<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form ref="userInfo" :model="userInfo" label-width="220px" :rules="rules">
      <!--手机 聘用形式  -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机" prop="mobile">
            <el-input v-model="userInfo.mobile" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="入职时间" prop="timeOfEntry">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              class="inputW"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <el-upload
              class="avatar-uploader"
              action="#"
              :show-file-list="false"
              :http-request="upload"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar">

              <i v-else class="el-icon-plus avatar-uploader-icon">
                <el-progress v-if="showProgress" type="circle" :percentage="percent" class="progress" />
              </i>

            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="hSaveUserDetailById">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { getUserDetailById, saveUserDetailById } from '@/api/departments'

import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDk03qMbZGhsmQnukIZaJlwrVsynMefPu8', // 身份识别ID
  SecretKey: 'yf5E6i38wShfNCl59xVnqdBUYMuCF2Zq' // 身份秘钥
})
// 导入局部组件必须在scrip里面导入,在App里面导入
export default {
// 组件名称
  name: '',
  // 接收局部注册的组件
  components: {},
  // 组件参数接收来自父组件的数据
  props: {},
  // 组件数据
  data () {
    return {
      userId: this.$route.query.id,
      userInfo: {
        mobile: '',
        timeOfEntry: ''
      },
      showProgress: false, // 进度条
      imageUrl: '',
      percent: '',
      rules: {
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  async mounted () {
    const res = await getUserDetailById(this.userId)
    // console.log(res.data)
    this.userInfo = res.data
    // this.userInfo.timeOfEntry = res.data.timeOfEntry
    this.imageUrl = res.data.staffPhoto
    // console.log(res.data.staffPhoto)
  },
  methods: {
    upload (res) {
      if (res.file) {
        // 显示
        this.showProgress = true
        // 执行上传操作
        cos.putObject({
          Bucket: 'yuancphoto-1307357740', /* 存储桶 */
          Region: 'ap-beijing', /* 存储桶所在地域，必须字段 */
          Key: res.file.name, /* 文件名 */
          StorageClass: 'STANDARD', // 上传模式, 标准模式
          Body: res.file, // 上传文件对象
          onProgress: (progressData) => {
            console.log(JSON.stringify(progressData))
            this.percent = progressData.percent * 100
          }
        }, (err, data) => {
          console.log(err || data)
          // 上传成功之后
          if (data.statusCode === 200) {
            this.imageUrl = `https:${data.Location}`
            this.userInfo.staffPhoto = `https:${data.Location}`
            // 隐藏
            this.showProgress = false
          }
        })
      }
    },
    // 保存更新
    hSaveUserDetailById () {
      this.$refs.userInfo.validate(async (valid) => {
        if (valid) {
          await saveUserDetailById(this.userId, this.userInfo)
          // console.log(res)
          this.$message.success('更新成功!')
        }
      })
    },
    // 上传之前的回调  全部满足才会执行
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }

}
</script>
<style lang='scss' scoped>
.user-info{
    padding-top:20px;
  }
 .user-info .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
 .user-info .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
 .user-info .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
 .user-info .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
